package com.shop.zkmall.app.service.a.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.zkmall.app.dao.product.CereShopProductDAO;
import com.shop.zkmall.app.domain.CereShopMarketing;
import com.shop.zkmall.app.domain.LiveRoomGoods;
import com.shop.zkmall.app.domain.req.LiveRoomGoodsReq;
import com.shop.zkmall.app.mapper.LiveRoomGoodsMapper;
import com.shop.zkmall.app.service.a.ILiveRoomGoodsService;
import com.shop.zkmall.commons.domain.product.CereShopProduct;
import com.shop.zkmall.commons.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 直播间商品表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2025-11-27
 */
@Service
public class LiveRoomGoodsServiceImpl extends ServiceImpl<LiveRoomGoodsMapper, LiveRoomGoods> implements ILiveRoomGoodsService {

    @Autowired
    private CereShopProductDAO cereShopProductDAO;

    @Autowired
    private CereShopMarketingServiceImpl cereShopMarketingService;

    @Override
    public R<?> addLiveProduct(LiveRoomGoodsReq liveRoomGoodsReq) {
        if (CollUtil.isEmpty(liveRoomGoodsReq.getGoodsId())){
            return R.fail("商品ID不能为空");
        }
        //查询商品信息
        LambdaQueryWrapper<CereShopMarketing> cereShopMarketingLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cereShopMarketingLambdaQueryWrapper.in(CereShopMarketing::getGoodsId,liveRoomGoodsReq.getGoodsId());
        List<CereShopMarketing> cereShopMarketings = cereShopMarketingService.list(cereShopMarketingLambdaQueryWrapper);

        //查询商品具体信息
        LambdaQueryWrapper<CereShopProduct> cereShopProductLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cereShopProductLambdaQueryWrapper.in(CereShopProduct::getProductId,liveRoomGoodsReq.getGoodsId());
        List<CereShopProduct> cereShopProducts = cereShopProductDAO.selectList(cereShopProductLambdaQueryWrapper);

        //插入数据
        List<LiveRoomGoods> ins = new ArrayList<>();
        for (int i = 0; i < cereShopMarketings.size(); i++) {
            LiveRoomGoods liveRoomGoods = new LiveRoomGoods();
            liveRoomGoods.setLiveRoomId(liveRoomGoodsReq.getLiveRoomId());
            liveRoomGoods.setGoodsId(liveRoomGoodsReq.getGoodsId().get(i));
            liveRoomGoods.setGoodsName(cereShopProducts.get(i).getProductName());
            liveRoomGoods.setCoverImg(cereShopProducts.get(i).getProductImage());
            ins.add(liveRoomGoods);
        }
        boolean b = this.saveBatch(ins);


        return R.ok(b);
    }

    @Override
    public R<?> delLiveProduct(LiveRoomGoodsReq liveRoomGoodsReq) {
        if (CollUtil.isEmpty(liveRoomGoodsReq.getGoodsId())){
            return R.fail("商品ID不能为空");
        }
        boolean b = this.remove(new LambdaQueryWrapper<LiveRoomGoods>().eq(LiveRoomGoods::getLiveRoomId, liveRoomGoodsReq.getLiveRoomId()).in(LiveRoomGoods::getGoodsId, liveRoomGoodsReq.getGoodsId()));
        return R.ok(b);
    }
}
